/**
 * Filename: SwitchPhotoActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/5/7 22:29
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.OvershootInterpolator;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.SwitchPhotosAdapter;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.utils.WrapContentGridLayoutManager;
import com.github.postsanf.yinian.view.SpacesItemDecoration;
import com.umeng.analytics.MobclickAgent;

import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;

public class SwitchPhotoActivity extends BaseActivity implements ISkinUpdate {
    private Toolbar mToolbar;
    private RecyclerView rv_switch_photo;
    private SwitchPhotosAdapter mAdapter;
    private View mAddPhotoView;

    //    private GridView            gv_switch_photo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_switch_photo);
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.switch_photo_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        mAddPhotoView = View.inflate(this, R.layout.yn_include_add_photo, null);
        mAddPhotoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SwitchPhotoActivity.this, LocalMusicManageActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                Intent mIntent = new Intent(ACTIONs.actionUpdateUpNum);
                sendBroadcast(mIntent);
            }
        });
        rv_switch_photo = (RecyclerView) findViewById(R.id.rv_switch_photo);
        SpacesItemDecoration decoration = new SpacesItemDecoration(3);
        rv_switch_photo.addItemDecoration(decoration);
        rv_switch_photo.setLayoutManager(new WrapContentGridLayoutManager(this, 3));
        // 设置适配器
        mAdapter = new SwitchPhotosAdapter(this, SelectedImg.mSelectedImage);
        mAdapter.setHeaderView(mAddPhotoView);
        rv_switch_photo.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));
        rv_switch_photo.getItemAnimator().setAddDuration(300);
        rv_switch_photo.getItemAnimator().setRemoveDuration(300);
        rv_switch_photo.getItemAnimator().setMoveDuration(300);
        rv_switch_photo.getItemAnimator().setChangeDuration(300);
        rv_switch_photo.setHasFixedSize(true);
        rv_switch_photo.setAdapter(mAdapter);
//        gv_switch_photo = (GridView) findViewById(R.id.gv_switch_photo);
//        SwitchPhotoAdapter gridViewAdapter = new SwitchPhotoAdapter(this,SelectedImg.mSelectedImage);
//        gv_switch_photo.setAdapter(gridViewAdapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_sure:
                Intent data = new Intent();
                setResult(RESULT_OK, data);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_sure, menu);
        return true;
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}